package com.formaplus.appcom14.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.formaplus.appcom14.entity.Society;
import com.formaplus.appcom14.utilitiares.Stat;

public class DAO_Society {

	private SQLiteDatabase _mDatabase;

	public DAO_Society(Context pContext){
		SqliteCreator s=new SqliteCreator(pContext, Stat.BASE_NAME, null, 2);
		_mDatabase=s.getWritableDatabase();

	}

	public void close(){
		_mDatabase.close();
	}

	public void insert(Society s){

		//_mDatabase.execSQL("INSERT INTO "+Stat.TABLE_NAME+" VALUES (.....");

		ContentValues values=new ContentValues();
		// pas la peinne car c un ID autocincrement values.put(Stat.COL_ID, 1);
		values.put(Stat.COL_Name,s.getName());
		values.put(Stat.COL_Add, s.getAddresse());
		values.put(Stat.COL_Latitude, s.getLatitude());
		values.put(Stat.COL_Longitude, s.getLongitude());
		values.put(Stat.COL_Phone, s.getPhone());
		_mDatabase.insert(Stat.TABLE_NAME, null, values);
	}

	public void deleteAll() {

		//_mDatabase.execSQL("DELETE FROM "+Stat.TABLE_NAME);

		_mDatabase.delete(Stat.TABLE_NAME, null, null);
	}

	public void delete(int pID){

		//_mDatabase.delete(Stat.TABLE_NAME, Stat.COL_ID+"=?", new String[]{""+pID});
		_mDatabase.delete(Stat.TABLE_NAME, Stat.COL_ID+"="+pID,null);
	}

	public void update(int pID,Society s){

		ContentValues values=new ContentValues();
		// pas la peinne car c un ID autocincrement values.put(Stat.COL_ID, 1);
		values.put(Stat.COL_Name,s.getName());
		values.put(Stat.COL_Add, s.getAddresse());
		values.put(Stat.COL_Latitude, s.getLatitude());
		values.put(Stat.COL_Longitude, s.getLongitude());
		values.put(Stat.COL_Phone, s.getPhone());

		_mDatabase.update(Stat.TABLE_NAME, values, Stat.COL_ID+"="+pID, null);

	}

	public void updateFav(int pID,int fav){

		ContentValues values=new ContentValues();
		values.put(Stat.COL_FAV, fav);	
		_mDatabase.update(Stat.TABLE_NAME, values, Stat.COL_ID+"="+pID, null);

	}

	public Cursor display(){

		// NONNNNN car execSqlt s'appliquer au requettes de type exex : update,delete	_mDatabase.execSQL("SELECT * FROM  Society");		
		String lCMD="SELECT ID as _id, * FROM  "+Stat.TABLE_NAME;
		return _mDatabase.rawQuery(lCMD, null);
	}
}
